package com.example.wac.controller;

import com.example.wac.constant.Constant;
import com.example.wac.service.SmsService;
import com.example.wac.vo.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author wuancheng
 * @description
 * @date 2022-04-21 14:10:00
 */

@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    SmsService smsService;

    @GetMapping("/send")
    public ApiResult sendSms(HttpServletRequest request, @RequestParam("mobile") String mobile){
        // 生成手机验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        Map<String, Object> map = new HashMap<>(2);
        map.put("code", code);
        boolean flag = smsService.sendSms(mobile, Constant.Sms.templateCode, map, request);
        if (!flag){
            return ApiResult.error(null,"短信发送失败");
        }
        return ApiResult.success();
    }

}
